﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using QLST_DTO;
using QLST_BUS;

namespace QLST_CoopMart_GoVap
{
    public partial class uc_BanHang : UserControl
    {
        HANGHOABUS hhBus = new HANGHOABUS();
        HANG_HOA hh = new HANG_HOA();
        KHACHHANGBUS KHbus = new KHACHHANGBUS();

        HOADONBUS HDBUS = new HOADONBUS();
        CTHOADONBUS CTHDBUS = new CTHOADONBUS();

        int stt = 0;
        float TongTien = 0f;
        int DongChon = -1;
        public uc_BanHang()
        {
            InitializeComponent();
        }

        public void LoadCombo()
        {
           
            CboSanPham.DisplayMember = "TenHang";
            CboSanPham.ValueMember = "MaHang";
            DataTable tbl = hhBus.GetSanPham();
            CboSanPham.DataSource = tbl; 
            CboSanPham.SelectedIndex = 0; 

            //Load khach hang
            cboHoTen.DisplayMember = "HoTen";
            cboHoTen.ValueMember = "MaKH";
            DataTable tbl1 = KHbus.Get();
            cboHoTen.DataSource = tbl1;
            cboHoTen.SelectedIndex = 0; 
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void uc_BanHang_Load(object sender, EventArgs e)
        {
            LoadCombo();
            //Load thong tin nhan vien
            lblNVBH.Text = "Nhân viên bán hàng: " + frmMain.NV.HoTen;
            lblNVBH.Tag = frmMain.NV.MaNV;
        }

        private void CboSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetError(CboSanPham, "");
            if (CboSanPham.SelectedIndex != -1)
            {
                hh = hhBus.Get((int)CboSanPham.SelectedValue);
                //Hien thi thong tin
                lblTen.Text = hh.TenHang;
                lblGia.Text = hh.GiaBan.ToString();
                cboDVT.Text = hh.DVT;
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
                e.Handled = true;
        }

        //public void LoadQuayHang(DataTable tblQuayHang)
        //{

        //    lvQuayHang.Items.Clear();

        //    int irow = 0;

        //    foreach (DataRow dr in tblQuayHang.Rows)
        //    {
        //        ListViewItem item = new ListViewItem("Quầy " + dr[0].ToString() + "\nCó " + dr["SoLuongSP"].ToString() + " sản phẩm");
        //        item.SubItems.Add(dr[1].ToString());
        //        item.SubItems.Add(dr[2].ToString());
        //        item.SubItems.Add(dr[3].ToString());
        //        item.SubItems.Add(dr[4].ToString());
        //        item.ImageIndex = 0;
        //        lvQuayHang.Items.Add(item);
        //        irow++;
        //    }
        //}

        public void AddHangHoa()
        {
            stt++;
            ListViewItem item = new ListViewItem(stt.ToString());
            item.SubItems.Add(hh.MaHang.ToString());
            item.SubItems.Add(hh.TenHang.ToString());
            item.SubItems.Add(txtSoLuong.Text);
            item.SubItems.Add(hh.GiaBan.ToString());
            item.SubItems.Add(hh.DVT.ToString());

            item.SubItems.Add((int.Parse(txtSoLuong.Text) * hh.GiaBan).ToString());
            lstHangHoa.Items.Add(item);
           
            CapNhatSTT();
        }
        void ShowError(Control ctrl, string ChuoiLoi, string TieuDe)
        {
            MessageBox.Show(ChuoiLoi, TieuDe, MessageBoxButtons.OK, MessageBoxIcon.Error);
            ctrl.Focus();
            
        }

        private int KiemTraLoi()
        {
            int fg = 1;
            if (txtSoLuong.Text.Trim() == "")
            {
                SetError(txtSoLuong, "Bạn vui lòng nhập số lượng");
                
                fg = 0;
            }
            if (CboSanPham.Text.Trim() == "")
            {
                SetError(CboSanPham, "Bạn chưa chọn hàng hóa");
                fg = 0;
            }
         
            if (cboDVT.Text.Trim() == "")
            {
                SetError(cboDVT, "Bạn chưa chọn đơn vị tính");
                fg = 0;
            }

            if (cboHoTen.Text.Trim() == "")
            {
                SetError(cboHoTen, "Khách hàng mặc định");
                cboHoTen.SelectedIndex = 0;
                fg = 0;
            }
            return fg;
        }

        public void SetError(Control ctrl, string Err)
        {

            error.SetError(ctrl, Err);
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (KiemTraLoi() == 0)
            {
                return;
            }
            ListViewItem item = KiemTraHangTonTai((int)CboSanPham.SelectedValue);
            if (item == null)
            {
                AddHangHoa();
            }
            else
            {
                if (MessageBox.Show("Mặt hàng đã có trong danh sách, bạn có muốn cập nhật số lượng không ? ", "Chọn trùng hàng",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int Tong = (int.Parse(item.SubItems[3].Text.ToString()) + int.Parse(txtSoLuong.Text.ToString()));
                    item.SubItems[3].Text = Tong.ToString();
                    item.SubItems[6].Text = (Tong * float.Parse(item.SubItems[4].Text.ToString())).ToString();
                    CapNhatTongTien();
                }
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            stt = 0;
            //Kiem tra gia tri
            if (KiemTraLoi() == 0)
                return;
            //Them hoa don\
            HOA_DON HD = new HOA_DON();
            HD.MaKH = (int)cboHoTen.SelectedValue;
            HD.MaNVThuNgan = (int)lblNVBH.Tag;
            HD.NgayLap = System.DateTime.Now;
            HD.TongTien = TongTien;
           int MaHD = HDBUS.Insert(HD);

            //Danh sach chi tiet
            CT_HOADON CT = new CT_HOADON();
            List<CT_HOADON> lst = new List<CT_HOADON>();
            foreach (ListViewItem item in lstHangHoa.Items)
            {
                CT = new CT_HOADON();
                CT.DonGia = int.Parse(item.SubItems[4].Text);
                CT.DVT = item.SubItems[5].ToString();
                CT.MaHang = int.Parse(item.SubItems[1].Text);
                CT.SoHoaDon = MaHD;
                CT.SoLuong = int.Parse(item.SubItems[3].Text);

                CTHDBUS.Insert(CT);
            }

        }
        public void CapNhatSTT()
        {
            stt = 1;
            TongTien = 0;
            foreach (ListViewItem item in lstHangHoa.Items)
            {
                item.SubItems[0].Text = stt.ToString();
                TongTien += float.Parse(item.SubItems[6].Text);
                stt++;
            }
            txtTienHang.Text = TongTien.ToString("#,##0");
        }

        public void CapNhatTongTien()
        {
            TongTien = 0;
            foreach (ListViewItem item in lstHangHoa.Items)
            {
                TongTien += float.Parse(item.SubItems[6].Text);
            }
            txtTienHang.Text = TongTien.ToString("#,##0");
        }

        public ListViewItem KiemTraHangTonTai(int MaHang)
        {
            foreach (ListViewItem item in lstHangHoa.Items)
            {
                if (item.SubItems[1].Text == MaHang.ToString())
                    return item;
            }
            return null;
        }
        private void btnXoahang_Click(object sender, EventArgs e)
        {
            ListViewItem item = lstHangHoa.FocusedItem;
            if (MessageBox.Show("Bạn có chắc chắn muốn xóa mặt hàng này không ? ", "Xóa",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                lstHangHoa.Items.Remove(item);
                CapNhatSTT();
            }
        }

        private void lstHangHoa_SelectedIndexChanged(object sender, EventArgs e)
        {
         
        }

        private void txtThue_TextChanged(object sender, EventArgs e)
        {
            if (txtThue.Text.Trim() == "")
            {
                txtThue.Text = "0";
            }
            if (txtTienHang.Text.Trim() != "")
            {
                txtTinhTienThue.Text = ((float.Parse(txtTienHang.Text)
                    * float.Parse(txtThue.Text)) / 100f).ToString();
            }
        }

        private void txtTienHang_TextChanged(object sender, EventArgs e)
        {
            if (txtThue.Text.Trim() != "")
            {
                txtTinhTienThue.Text = ((float.Parse(txtTienHang.Text)
                    * float.Parse(txtThue.Text)) / 100f).ToString();
                txtTongCong.Text = (float.Parse(txtTienHang.Text) +
                    float.Parse(txtTinhTienThue.Text)).ToString();

            }
        }

        private void txtThue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
                e.Handled = true;
        }

        private void txtTienKHDua_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)))
                e.Handled = true;
        }

        private void btnCapNhatSL_Click(object sender, EventArgs e)
        {
            ListViewItem item = lstHangHoa.FocusedItem;
            frmSuaSoLuong frm = new frmSuaSoLuong();
            frm.ShowDialog();
            if (frm.txtSoLuong.Text != "")
            {
                item.SubItems[3].Text = frm.txtSoLuong.Text;
                item.SubItems[6].Text = 
                    (int.Parse(item.SubItems[3].Text) * float.Parse(item.SubItems[4].Text)).ToString();
                    
                CapNhatTongTien();
            }
        }

        private void txtSoLuong_TextChanged(object sender, EventArgs e)
        {
            if (txtSoLuong.Text.Trim() != "")
            {
                SetError(txtSoLuong, "");
            }
        }

        private void cboDVT_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboDVT.Text.Trim() != "")
            {
                SetError(cboDVT, "");
            }
        }

        private void txtTinhTienThue_TextChanged(object sender, EventArgs e)
        {
            if (txtThue.Text.Trim() == "")
            {
                txtThue.Text = "0";
            }
        }

        private void txtTienKHDua_TextChanged(object sender, EventArgs e)
        {
            if (txtTienKHDua.Text.Trim() == "")
            {
                txtThue.Text = "0";
            }
           
            txtTienTraLai.Text = (float.Parse(txtTienKHDua.Text) - 
                float.Parse(txtTongCong.Text)).ToString("#,##0");
        }
        /// <summary>
        /// Xoa du lieu trong control de them haon do moi
        /// </summary>
        /// <returns></returns>
        private void XoaTrang()
        {
            cboHoTen.SelectedIndex = -1;
            CboSanPham.SelectedIndex = -1;
            txtSoLuong.Text = "0";
            txtTienHang.Text = "0";
            txtTienKHDua.Text = "0";
            txtTinhTienThue.Text = "0";
            txtTongCong.Text = "0";
            txtTienTraLai.Text = "0";
            txtDiaChi.Clear();
            txtDienthoai.Clear();
            //Xoa luoi
            lstHangHoa.Items.Clear();
        }

        private void cboHoTen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboHoTen.SelectedIndex != -1)
            {
                KHACH_HANG kh = KHbus.Get((int)cboHoTen.SelectedValue);
                txtDiaChi.Text = kh.DiaChi;
            }

        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            XoaTrang();
        }
    }
}
